﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services.Protocols;
using System.Xml;
using System.Drawing;


namespace MSU_UserControls
{
    [System.ComponentModel.Description("MSU Moodle Data")]
    public partial class MoodleData : MSU_UserControl, SmartPart.IConnectionConsumerControl {


        #region IConnectionConsumerControl Members

        public string ConsumerMenuLabel
        {
            get { return "Receives text data from"; }
        }

        public void SetConsumerData(object data)
        {
            if (data.GetType() == typeof(bool))
            {
                    if (!(bool)data)
                    {
                        lblLogOut.Visible = true;
                        lblMessage.Visible = false;
                        UnloadCourse();
                    }
                    else
                    {
                        lblMessage.Visible = true;
                        lblLogOut.Visible = false;
                        if(gvCourses.Visible != true && lkbReturn.Visible!=true)
                            LoadCourse();
                    }
            }
        }

        #endregion   

        #region Properties(CurrentCourse)

        public string CurrentCourse
        {
            get
            {
                if (ViewState["currentCourse"] == null)
                    return null;
                else
                    return ViewState["currentCourse"].ToString();
            }
            set { ViewState["currentCourse"] = value; }
        } 

        #endregion

        #region Events(Page_Load, gvCourses_SelectedIndexChanged, lkbReturn_Click)

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && _msu_User!=null)
            {

                LoadCourse();

            }

        }

        protected void gvCourses_SelectedIndexChanged(object sender, EventArgs e)
        {
            try{
            CurrentCourse = gvCourses.SelectedDataKey.Value.ToString();

            string role = "";

            DataTable dtGroups = WSMSU.LoadCourseData(_msu_User, _msu_Adm, CurrentCourse, ref role);

            lblRole.Text = role;

            bool noCourses;

            if (dtGroups.Rows.Count > 0)
                noCourses = false;
            else
                noCourses = true;

            ShowHideControls(false, noCourses);

            gvGroups.DataSource = dtGroups;
            gvGroups.DataBind();
            }
            catch (SoapException soapEx)
            {
                //Load the Detail element of the SoaopException object
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(soapEx.Detail.OuterXml);

                XmlNamespaceManager nsManager = new
                XmlNamespaceManager(doc.NameTable);

                // Add the namespace to the NamespaceManager
                nsManager.AddNamespace("errorNS", "http://tempuri.org/MSUWS");

                XmlNode categoryNode = doc.DocumentElement.SelectSingleNode("errorNS:Error", nsManager);
                string errorTypeNumber = categoryNode.SelectSingleNode("errorNS:ErrorTypeNumber", nsManager).InnerText;
                string message = categoryNode.SelectSingleNode("errorNS:Message", nsManager).InnerText;
                string stackTrace = categoryNode.SelectSingleNode("errorNS:StackTrace", nsManager).InnerText;
                string innerError = categoryNode.SelectSingleNode("errorNS:InnerError", nsManager).InnerText;
                string onScreen = categoryNode.SelectSingleNode("errorNS:OnScreen", nsManager).InnerText;

                if (string.Compare(onScreen, true.ToString()) == 0)
                {
                    lblResult.Text += message + "\r\n";
                    lblResult.ForeColor = Color.DarkRed;
                }
                else
                {
                    lblResult.Text += "Unexpected error. Details:\r\n";
                    lblResult.Text += "Soap fault code: " + soapEx.Code.ToString() + "\r\n";
                    lblResult.Text += "ErrorType Number is: " + errorTypeNumber + "\r\n";
                    lblResult.Text += "Error Message is: " + message + "\r\n";
                    lblResult.Text += "Error StackTrace is: " + stackTrace + "\r\n";
                    lblResult.Text += "Error InnerError is: " + (string.IsNullOrEmpty(innerError) ? "null" : innerError) + "\r\n";
                    lblResult.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                lblResult.Text += "Unexpected error. Details:\r\n";
                lblResult.Text += "Error Message is: " + ex.Message + "\r\n";
                lblResult.Text += "Error StackTrace is: " + ex.StackTrace + "\r\n";
                lblResult.Text += "Error InnerException is: " + ((ex.InnerException == null) ? "None" : ex.InnerException.Message) + "\r\n";
                lblResult.ForeColor = Color.Red;
            }
        }

        protected void lkbReturn_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dtCourses = WSMSU.LoadCourse(_msu_User);

                gvCourses.DataSource = dtCourses;
                gvCourses.DataBind();

                ShowHideControls(true, false);
            }
            catch (SoapException soapEx)
            {
                //Load the Detail element of the SoaopException object
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(soapEx.Detail.OuterXml);

                XmlNamespaceManager nsManager = new
                XmlNamespaceManager(doc.NameTable);

                // Add the namespace to the NamespaceManager
                nsManager.AddNamespace("errorNS", "http://tempuri.org/MSUWS");

                XmlNode categoryNode = doc.DocumentElement.SelectSingleNode("errorNS:Error", nsManager);
                string errorTypeNumber = categoryNode.SelectSingleNode("errorNS:ErrorTypeNumber", nsManager).InnerText;
                string message = categoryNode.SelectSingleNode("errorNS:Message", nsManager).InnerText;
                string stackTrace = categoryNode.SelectSingleNode("errorNS:StackTrace", nsManager).InnerText;
                string innerError = categoryNode.SelectSingleNode("errorNS:InnerError", nsManager).InnerText;

                lblResult.Text += soapEx.Code.ToString() + "\r\n";
                lblResult.Text += "ErrorType Number is: " + errorTypeNumber + "\r\n";
                lblResult.Text += "Error Message is: " + message + "\r\n";
                lblResult.Text += "Error StackTrace is: " + stackTrace + "\r\n";
                lblResult.Text += "Error InnerError is: " + (string.IsNullOrEmpty(innerError) ? "null" : innerError) + "\r\n";
                lblResult.ForeColor = Color.Red;
            }
            catch (Exception ex)
            {
                lblResult.Text = "Error: " + ex.Message;
                lblResult.ForeColor = Color.Red;
            }            
        }

        #endregion

        #region Methods(LoadCourse, UnloadCourse, ShowHideControls)

        private void LoadCourse()
        {
            try
            {
                DataTable dtCourses = WSMSU.LoadCourse(_msu_User);

                gvCourses.DataSource = dtCourses;
                gvCourses.DataBind();

                ShowHideControls(true, false);
            }
            catch (SoapException soapEx)
            {
                //Load the Detail element of the SoaopException object
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(soapEx.Detail.OuterXml);

                XmlNamespaceManager nsManager = new
                XmlNamespaceManager(doc.NameTable);

                // Add the namespace to the NamespaceManager
                nsManager.AddNamespace("errorNS", "http://tempuri.org/MSUWS");

                XmlNode categoryNode = doc.DocumentElement.SelectSingleNode("errorNS:Error", nsManager);
                string errorTypeNumber = categoryNode.SelectSingleNode("errorNS:ErrorTypeNumber", nsManager).InnerText;
                string message = categoryNode.SelectSingleNode("errorNS:Message", nsManager).InnerText;
                string stackTrace = categoryNode.SelectSingleNode("errorNS:StackTrace", nsManager).InnerText;
                string innerError = categoryNode.SelectSingleNode("errorNS:InnerError", nsManager).InnerText;

                lblResult.Text += soapEx.Code.ToString() + "\r\n";
                lblResult.Text += "ErrorType Number is: " + errorTypeNumber + "\r\n";
                lblResult.Text += "Error Message is: " + message + "\r\n";
                lblResult.Text += "Error StackTrace is: " + stackTrace + "\r\n";
                lblResult.Text += "Error InnerError is: " + (string.IsNullOrEmpty(innerError) ? "null" : innerError) + "\r\n";
                lblResult.ForeColor = Color.Red;
            }
            catch (Exception ex)
            {
                lblResult.Text = "Error: " + ex.Message;
                lblResult.ForeColor = Color.Red;
            }
        }

        private void UnloadCourse()
        {
            gvCourses.Visible = false;
            gvGroups.Visible = false;
            lblRole.Visible = false;
            lkbReturn.Visible = false;
            lblNoGroups.Visible = false;
        }

        private void ShowHideControls(bool showCourse, bool showNoGroups)
        {
            gvCourses.Visible = showCourse;
            gvGroups.Visible = !showCourse;
            lblRole.Visible = !showCourse;
            lkbReturn.Visible = !showCourse;
            lblNoGroups.Visible = showNoGroups;
        }
        
        #endregion

    }
}